package com.content.Service.Impl.drama;

import com.common.Utils.JwtUtil;
import com.content.Entity.drama.Audio;
import com.content.Entity.drama.BookMake;
import com.content.Entity.drama.Drama;
import com.content.Entity.drama.Video;
import com.content.Mapper.drama.DramaMapper;
import com.content.Service.drama.DramaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Service
public class DramaServiceImpl implements DramaService {
    @Autowired
    private DramaMapper DramaMapper;

    @Override
    public PageInfo<Drama> getDramaList(Integer pageNum, Integer pageSize, String dramaName) {
        PageHelper.startPage(pageNum,pageSize);
        List<Drama> dramaList = DramaMapper.getDramaList(dramaName);
        for (Drama drama : dramaList) {
            List<Video> videoList = DramaMapper.getVideoList(drama.getDramaId());
            List<Audio> audioList = DramaMapper.getAudioList(drama.getDramaId());
            drama.setAudioList(audioList);
            drama.setVideoList(videoList);
        }

        return new PageInfo<>(dramaList);
    }

    @Override
    public Drama getDrama(String dramaId) {
            Drama drama = DramaMapper.getDrama(dramaId);
            List<Video> videoList = DramaMapper.getVideoList(dramaId);
            List<Audio> audioList = DramaMapper.getAudioList(dramaId);
            drama.setAudioList(audioList);
            drama.setVideoList(videoList);
            return drama;
    }

    @Override
    public List<BookMake> getBookMakes(String userId) {
        return DramaMapper.getBookMakes(userId);

    }

    @Override
    public void addBookMake(BookMake bookMake) {
        bookMake.setBookmarkId(makeBId());
        bookMake.setBookmarkTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        DramaMapper.addBookMake(bookMake);
    }

    @Override
    public void deleteBookMake(BookMake bookMake) {
        String userId = bookMake.getUserId();
        String dramaId = bookMake.getDramaId();
        DramaMapper.deleteBookMake(dramaId, userId);
    }

    @Override
    public PageInfo<BookMake> getFavorites(Integer pageNum, Integer pageSize, String sortType, String userId) {
        PageHelper.startPage(pageNum,pageSize);
        if (sortType.equals("time")) {
            List<BookMake> bookMakes = DramaMapper.getBookMakes1(userId);
            return new PageInfo<>(bookMakes);
        }else if (sortType.equals("name")) {
            List<BookMake> bookMakes = DramaMapper.getBookMakes2(userId);
            return new PageInfo<>(bookMakes);
        }else {
            List<BookMake> bookMakes = DramaMapper.getBookMakes3(userId);
            return new PageInfo<>(bookMakes);
        }
    }

    @Override
    public PageInfo<Drama> getDramaListByThree(Integer pageNum, Integer pageSize, String dramaName, String username) {
        PageHelper.startPage(pageNum,pageSize);
        List<Drama> dramaList = DramaMapper.getDramaListByThree(dramaName, username);
        for (Drama drama : dramaList) {
            List<Video> videoList = DramaMapper.getVideoList(drama.getDramaId());
            List<Audio> audioList = DramaMapper.getAudioList(drama.getDramaId());
            drama.setAudioList(audioList);
            drama.setVideoList(videoList);
        }

        return new PageInfo<>(dramaList);
    }

    @Override
    public void addDrama(Drama drama) {
        String token = drama.getToken();
        String username = JwtUtil.extractUsername(token);
        drama.setCreateUser(username);
        drama.setCreateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        drama.setDramaId(makeId());
        DramaMapper.addDrama(drama);
    }

    private String makeVId() {
        return "DV" + System.currentTimeMillis();
    }
    private String makeAId() {
        return "DA" + System.currentTimeMillis();
    }
    private String makeBId() {
        return "B" + System.currentTimeMillis();
    }

    private String makeId() {
        return "D" + System.currentTimeMillis();
    }

    @Override
    public void updateDrama(Drama drama) {
        String token = drama.getToken();
        String username = JwtUtil.extractUsername(token);
        drama.setUpdateUser(username);
        drama.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        DramaMapper.updateDrama(drama);
    }

    @Override
    public void deleteDrama(String dramaId) {
        DramaMapper.deleteDrama(dramaId);
        List<Video> videoList = DramaMapper.getVideoList(dramaId);
        List<Audio> audioList = DramaMapper.getAudioList(dramaId);
        for (Video video : videoList) {
            DramaMapper.deleteVideo(video.getVideoId());
        }
        for (Audio audio : audioList) {
            DramaMapper.deleteAudio(audio.getAudioId());
        }
        //删除收藏剧目
        DramaMapper.deleteBookMakeByDramaId(dramaId);
    }


}
